import { request } from "../../request/index"
// pages/cart/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: {},
        cartGoodList: [],
        allChecked: false,
        totalPrice: 0,
        numTotal: 0
    },


    addAddress() {
        wx.chooseAddress({
            success: (res) => {
                wx.setStorageSync("address", res);
                this.setData({
                    address: res
                })
            },

        });
    },
    pay() {
        if (!wx.getStorageSync("address").userName) {
            wx.showToast({
                title: '请您先添加收货地址',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (this.data.numTotal == 0) {
            wx.showToast({
                title: '请您选择要购买的商品',
                icon: 'none',
                duration: 1000
            })
            return
        }
        let token = wx.getStorageSync("token")
        if (!token) {
            wx.navigateTo({
                url: '/pages/auth/index',
                success: (result) => {

                },
                fail: () => { },
                complete: () => { }
            });
            return
        }
        let goods = []
        this.data.cartGoodList.forEach(v => goods.push(
            {
                goods_id: v.goods_id,
                goods_number: v.num,
                goods_price: v.goods_price
            }
        ))
        request({
            url: "/my/orders/create", method: "POST", header: { Authorization: token }, data: {
                order_number: this.data.totalPrice,
                consignee_addr: this.data.address,
                goods
            }
        }).then(res => {
            console.log(res);
        })
        request({ url: "/my/orders/chkOrder", method: "POST", order_number: "22229872233578987654200008" }).then(res => {
            console.log(res);

        })
        request({ url: "/my/orders/req_unifiedorder", method: "POST", header: { Authorization: token }, order_number: "22229872233578987654200008" }).then(res => {
            console.log(res);
            let deviceIds = goods.map(item => item.goods_id)
            let needMatchDevices = wx.getStorageSync("cartGoodList").filter(item => !deviceIds.includes(item.goods_id))
            wx.setStorageSync('cartGoodList', needMatchDevices)
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // this.data.cartGoodList = wx.getStorageSync('cartGoodList').filter(i => i.checked)
        if (wx.getStorageSync("payGood")) {
            let total = wx.getStorageSync("payGood").reduce((total, v) => total += parseFloat(v.goods_price) * v.num, 0);
            this.setData({
                totalPrice: total,
                cartGoodList: wx.getStorageSync("payGood")
            })
        }
        this.setData({
            address: wx.getStorageSync("address"),
            allChecked: wx.getStorageSync("payGood").length > 0 ? wx.getStorageSync("payGood").every(v => v.checked == true) : false,
        })
        // let a = this.data.cartGoodList.filter(i => i.checked == true)
        // console.log(a);
        this.data.numTotal = 0
        for (let i of wx.getStorageSync("payGood")) {
            this.data.numTotal += i.num
        }
        this.setData({
            numTotal: this.data.numTotal
        })

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})